package com.molichuxing.service.impl;

import com.molichuxing.framework.utils.CollectionUtils;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.RedisUtil;
import com.molichuxing.property.BuyBackEnum;
import com.molichuxing.service.StatisticsService;
import com.molichuxing.services.business.dto.response.DealerStoreInfoBizDto;
import com.molichuxing.services.business.dto.response.StatisticsAssetOrderDto;
import com.molichuxing.services.business.dto.response.VehicleNameBizDto;
import com.molichuxing.services.business.service.GoodsBizService;
import com.molichuxing.services.business.service.OrderExclusivelBizService;
import com.molichuxing.services.business.service.StoreBizService;
import com.molichuxing.services.business.service.VehicleBizService;
import com.molichuxing.services.infrastructure.dto.request.create.StatisticsAssetCreateDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.infrastructure.service.*;
import com.molichuxing.services.property.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: zhaopenghui
 * @Description:
 * @Date: Created in 17:04 2020/08/24
 * @Modified By:
 **/
@Service("statisticsService")
public class StatisticsServiceImpl implements StatisticsService {

    private static final Logger log = Logger.getLogger(StatisticsServiceImpl.class);

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private OrderExclusivelService orderExclusivelService;
    @Resource
    private VehicleService vehicleService;
    @Resource
    private StatisticsAssetService statisticsAssetService;
    @Resource
    private RegionService regionService;
    @Resource
    private EntrepotStorageService entrepotStorageService;
    @Resource
    private StockStoreService stockStoreService;
    @Resource
    private StoreService storeService;
    @Resource
    private OrderExclusivelBizService orderExclusivelBizService;
    @Resource
    private CarService carService;
    @Resource
    private UserService userService;
    @Resource
    private StoreBizService storeBizService;
    @Resource
    private VehicleBizService vehicleBizService;
    @Resource
    private GoodsBizService goodsBizService;


    /**
     * 资产统计
     *
     * @return
     * @throws Exception
     */
    @Override
    public Boolean getAsset() throws Exception {
        //删除数据
        deleteAsset();

        //获取说有车型
        Map<Integer, String> allVehicleNameMap = getAllVehicleNameMap();
        //得到所有的城市名称
        Map<Integer, String> city = getAllCityMap();
        //所有门店仓储
        Map<Integer, Integer> storeMap = stockStoreService.getAllStockStore();
        //获取所有门店
        Map<Integer, StoreDto> allStoreByCarsMap = getAllStoreByCarsMap();
        //all中转库
        Map<Integer, EntrepotStorageDto> entrepotStorage = getEntrepotStorage();


        //全款买断
        getFullBuyOut();
        //运营车辆
        getOperatingVehicle();
        //二次运营
        getSecondaryOperations();
        //销售
        getSell();
        //在库-实际在库
        getInStockActuallyInStock();
        //在途车辆
        getInRoadVehicle();
        //在库-诉讼中
        getInStockInLawsuit();
        //报废车
        getAbandonedVehicle(allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        //丢失车
        getLoseVehicle(allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        //待收车
        getStayBackVehicle(allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        //逾期及特殊-诉讼中
        getOverdueAndSpecialInLawsuit(allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        //批售

        getBulkSales(allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        //实际在库
        getActuallyInStock(allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        //公务车
        getOfficialVehicle(allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        return true;
    }

    /**
     * 获取所有中转库
     *
     * @return
     */
    private Map<Integer, EntrepotStorageDto> getEntrepotStorage() {
        List<EntrepotStorageDto> list = entrepotStorageService.getEntrepotStorageList();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        } else {
            return list.stream().collect(Collectors.toMap(EntrepotStorageDto::getId, entrepotStorageDto -> entrepotStorageDto));
        }
    }

    /**
     * 获取门店信息
     *
     * @return
     */
    private Map<Integer, StoreDto> getAllStoreByCarsMap() throws Exception {
        /**
         * 获取所有门店
         */
        List<StoreDto> allStoreList = storeService.getAllStoreList(null);
        if (CollectionUtils.isEmpty(allStoreList)) {
            return Collections.emptyMap();
        }
        return allStoreList.stream().collect(Collectors.toMap(StoreDto::getStoreId, storeDto -> storeDto));
    }

    ;

    /**
     * 得到所有城市
     *
     * @return
     */
    private Map<Integer, String> getAllCityMap() {
        List<RegionDto> all = regionService.getAll();
        if (CollectionUtils.isEmpty(all)) {
            return null;
        } else {
            Map<Integer, String> collect = all.stream().collect(Collectors.toMap(RegionDto::getId, RegionDto::getShortName));
            return collect;
        }

    }

    /**
     * 获取全部车型信息
     *
     * @return
     */
    private Map<Integer, String> getAllVehicleNameMap() {
        List<VehicleNameBizDto> all = vehicleService.getAllVehicleMake();
        if (CollectionUtils.isEmpty(all)) {
            return Collections.emptyMap();
        } else {
            Map<Integer, String> collect = all.stream().collect(Collectors.toMap(VehicleNameBizDto::getModelId,
                    vehicleNameBizDto -> vehicleNameBizDto.getSeriesName() + "-" + vehicleNameBizDto.getModelName()));
            return collect;
        }
    }

    /**
     * 删除数据
     *
     * @return
     */
    private Boolean deleteAsset() throws Exception {
        return statisticsAssetService.deleteAsset();
    }

    /**
     * 全款买断
     *
     * @return
     */
    private Boolean getFullBuyOut() throws Exception {
        List<CarAssetDto> carAssetDtoList = carService.getFullBuyOut();
        Integer useTypeValue = StatisticsAssetUseTypeEnum.RENTAL.getValue();
        Integer rentalTypeValue = StatisticsAssetRentalTypeEnum.FULL_BUY_OUT.getValue();
        Integer rollbackOverdueTypeValue = null;
        Integer inStockTypeValue = null;
        List<StatisticsAssetCreateDto> statisticsAssetList = getStatisticsAssetList(carAssetDtoList, useTypeValue, rentalTypeValue, rollbackOverdueTypeValue, inStockTypeValue);
        return createStatisticsAsset(statisticsAssetList);
    }


    /**
     * 运营车辆
     *
     * @return
     */
    private Boolean getOperatingVehicle() throws Exception {
        List<CarAssetDto> carAssetDtoList = carService.getOperatingVehicle();
        Integer useTypeValue = StatisticsAssetUseTypeEnum.RENTAL.getValue();
        Integer rentalTypeValue = StatisticsAssetRentalTypeEnum.OPERATING_VEHICLE.getValue();
        Integer rollbackOverdueTypeValue = null;
        Integer inStockTypeValue = null;
        List<StatisticsAssetCreateDto> statisticsAssetList = getStatisticsAssetList(carAssetDtoList, useTypeValue, rentalTypeValue, rollbackOverdueTypeValue, inStockTypeValue);
        return createStatisticsAsset(statisticsAssetList);
    }

    /**
     * 二次运营
     *
     * @return
     */
    private Boolean getSecondaryOperations() throws Exception {
        List<CarAssetDto> carAssetDtoList = carService.getSecondaryOperations();
        Integer useTypeValue = StatisticsAssetUseTypeEnum.RENTAL.getValue();
        Integer rentalTypeValue = StatisticsAssetRentalTypeEnum.ROLLBACK_VEHICLE.getValue();
        Integer rollbackOverdueTypeValue = StatisticsAssetRollbackOverdueTypeEnum.SECONDARY_OPERATIONS.getValue();
        Integer inStockTypeValue = null;
        List<StatisticsAssetCreateDto> statisticsAssetList = getStatisticsAssetList(carAssetDtoList, useTypeValue, rentalTypeValue, rollbackOverdueTypeValue, inStockTypeValue);
        return createStatisticsAsset(statisticsAssetList);
    }

    /**
     * 销售
     *
     * @return
     */
    private Boolean getSell() throws Exception {
        List<CarAssetDto> carAssetDtoList = carService.getSell();
        Integer useTypeValue = StatisticsAssetUseTypeEnum.RENTAL.getValue();
        Integer rentalTypeValue = StatisticsAssetRentalTypeEnum.ROLLBACK_VEHICLE.getValue();
        Integer rollbackOverdueTypeValue = StatisticsAssetRollbackOverdueTypeEnum.SELL.getValue();
        Integer inStockTypeValue = null;
        List<StatisticsAssetCreateDto> statisticsAssetList = getStatisticsAssetList(carAssetDtoList, useTypeValue, rentalTypeValue, rollbackOverdueTypeValue, inStockTypeValue);
        return createStatisticsAsset(statisticsAssetList);
    }

    /**
     * 在库-实际在库
     *
     * @return
     */
    private Boolean getInStockActuallyInStock() throws Exception {
        List<CarAssetDto> carAssetDtoList = carService.getInStockActuallyInStock();
        Integer useTypeValue = StatisticsAssetUseTypeEnum.RENTAL.getValue();
        Integer rentalTypeValue = StatisticsAssetRentalTypeEnum.ROLLBACK_VEHICLE.getValue();
        Integer rollbackOverdueTypeValue = StatisticsAssetRollbackOverdueTypeEnum.IN_STOCK.getValue();
        Integer inStockTypeValue = StatisticsAssetInStockTypeEnum.IN_STOCK_ACTUALLY_INSTOCK.getValue();
        List<StatisticsAssetCreateDto> statisticsAssetList = getStatisticsAssetList(carAssetDtoList, useTypeValue, rentalTypeValue, rollbackOverdueTypeValue, inStockTypeValue);
        return createStatisticsAsset(statisticsAssetList);
    }

    /**
     * 在途车辆
     *
     * @return
     */
    private Boolean getInRoadVehicle() throws Exception {
        List<CarAssetDto> carAssetDtoList = carService.getInRoadVehicle();
        Integer useTypeValue = StatisticsAssetUseTypeEnum.RENTAL.getValue();
        Integer rentalTypeValue = StatisticsAssetRentalTypeEnum.ROLLBACK_VEHICLE.getValue();
        Integer rollbackOverdueTypeValue = StatisticsAssetRollbackOverdueTypeEnum.IN_STOCK.getValue();
        Integer inStockTypeValue = StatisticsAssetInStockTypeEnum.IN_ROAD_VEHICLE.getValue();
        List<StatisticsAssetCreateDto> statisticsAssetList = getStatisticsAssetList(carAssetDtoList, useTypeValue, rentalTypeValue, rollbackOverdueTypeValue, inStockTypeValue);
        return createStatisticsAsset(statisticsAssetList);
    }

    /**
     * 在库-诉讼中
     *
     * @return
     */
    private Boolean getInStockInLawsuit() throws Exception {
        List<CarAssetDto> carAssetDtoList = carService.getInStockInLawsuit();
        Integer useTypeValue = StatisticsAssetUseTypeEnum.RENTAL.getValue();
        Integer rentalTypeValue = StatisticsAssetRentalTypeEnum.ROLLBACK_VEHICLE.getValue();
        Integer rollbackOverdueTypeValue = StatisticsAssetRollbackOverdueTypeEnum.IN_STOCK.getValue();
        Integer inStockTypeValue = StatisticsAssetInStockTypeEnum.IN_STOCK_IN_LAWSUIT.getValue();
        List<StatisticsAssetCreateDto> statisticsAssetList = getStatisticsAssetList(carAssetDtoList, useTypeValue, rentalTypeValue, rollbackOverdueTypeValue, inStockTypeValue);
        return createStatisticsAsset(statisticsAssetList);
    }

    /**
     * 报废车
     *
     * @param allVehicleNameMap
     * @param city
     * @param storeMap
     * @param allStoreByCarsMap
     * @param entrepotStorage
     * @return
     */
    private Boolean getAbandonedVehicle(Map<Integer, String> allVehicleNameMap, Map<Integer, String> city, Map<Integer, Integer> storeMap, Map<Integer, StoreDto> allStoreByCarsMap, Map<Integer, EntrepotStorageDto> entrepotStorage) {
        List<StatisticsAssetOrderDto> order = statisticsAssetService.getScrappedCarStatisticsAssetOrder();
        log.info("查询报废车数据:{}" + order);
        List<StatisticsAssetCreateDto> dtoList = toStatisticsAssetCreateDtoList(order, allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        if (CollectionUtils.isEmpty(dtoList)) {
            return true;
        } else {
            statisticsAssetService.saveBatchStatisticsAsset(dtoList);
        }
        return true;
    }

    /**
     * 丢失车
     *
     * @param allVehicleNameMap
     * @param city
     * @param storeMap
     * @param allStoreByCarsMap
     * @param entrepotStorage
     * @return
     */
    private Boolean getLoseVehicle(Map<Integer, String> allVehicleNameMap, Map<Integer, String> city, Map<Integer, Integer> storeMap, Map<Integer, StoreDto> allStoreByCarsMap, Map<Integer, EntrepotStorageDto> entrepotStorage) {
        List<StatisticsAssetOrderDto> order = statisticsAssetService.getLostCarStatisticsAssetOrder();
        log.info("查询丢失车数据:{}" + order);
        List<StatisticsAssetCreateDto> dtoList = toStatisticsAssetCreateDtoList(order, allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        if (CollectionUtils.isEmpty(dtoList)) {
            return true;
        } else {
            statisticsAssetService.saveBatchStatisticsAsset(dtoList);
        }
        return true;
    }

    /**
     * 待收车
     *
     * @param allVehicleNameMap
     * @param city
     * @param storeMap
     * @param allStoreByCarsMap
     * @param entrepotStorage
     * @return
     */
    private Boolean getStayBackVehicle(Map<Integer, String> allVehicleNameMap, Map<Integer, String> city, Map<Integer, Integer> storeMap, Map<Integer, StoreDto> allStoreByCarsMap, Map<Integer, EntrepotStorageDto> entrepotStorage) {
        List<StatisticsAssetOrderDto> order = statisticsAssetService.getWaitingCarStatisticsAssetOrderDto();
        log.info("查询待收车数据:{}" + order);
        List<StatisticsAssetCreateDto> dtoList = toStatisticsAssetCreateDtoList(order, allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        if (CollectionUtils.isEmpty(dtoList)) {
            return true;
        } else {
            statisticsAssetService.saveBatchStatisticsAsset(dtoList);
        }
        return true;
    }

    /**
     * 逾期及特殊-诉讼中
     *
     * @param allVehicleNameMap
     * @param city
     * @param storeMap
     * @param allStoreByCarsMap
     * @param entrepotStorage
     * @return
     */
    private Boolean getOverdueAndSpecialInLawsuit(Map<Integer, String> allVehicleNameMap, Map<Integer, String> city, Map<Integer, Integer> storeMap, Map<Integer, StoreDto> allStoreByCarsMap, Map<Integer, EntrepotStorageDto> entrepotStorage) {
        List<StatisticsAssetOrderDto> order = statisticsAssetService.getLawsuitCarStatisticsAssetOrderDto();
        log.info("查询逾期及特殊-诉讼中数据:{}" + order);
        List<StatisticsAssetCreateDto> dtoList = toStatisticsAssetCreateDtoList(order, allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        if (CollectionUtils.isEmpty(dtoList)) {
            return true;
        } else {
            statisticsAssetService.saveBatchStatisticsAsset(dtoList);
        }
        return true;
    }

    /**
     * 批售
     *
     * @param allVehicleNameMap
     * @param city
     * @param storeMap
     * @param allStoreByCarsMap
     * @param entrepotStorage   所有中转库
     * @return
     */
    private Boolean getBulkSales(Map<Integer, String> allVehicleNameMap, Map<Integer, String> city, Map<Integer, Integer> storeMap, Map<Integer, StoreDto> allStoreByCarsMap, Map<Integer, EntrepotStorageDto> entrepotStorage) {

        List<StatisticsAssetOrderDto> order = statisticsAssetService.getBatchSaleCarStatisticsAssetOrderDto();
        log.info("查询批售中数据:{}" + order);
        List<StatisticsAssetCreateDto> dtoList = toStatisticsAssetCreateDtoList(order, allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        if (CollectionUtils.isEmpty(dtoList)) {
            return true;
        } else {
            statisticsAssetService.saveBatchStatisticsAsset(dtoList);
        }
        return true;


    }

    /**
     * 实际在库
     *
     * @param allVehicleNameMap
     * @param city
     * @param storeMap
     * @param allStoreByCarsMap
     * @param entrepotStorage
     * @return
     */
    private Boolean getActuallyInStock(Map<Integer, String> allVehicleNameMap, Map<Integer, String> city, Map<Integer, Integer> storeMap, Map<Integer, StoreDto> allStoreByCarsMap, Map<Integer, EntrepotStorageDto> entrepotStorage) {
        List<StatisticsAssetOrderDto> order = statisticsAssetService.getInCarStoreStatisticsAssetOrderDto();
        log.info("查询实际在库数据:{}" + order);
        List<StatisticsAssetCreateDto> dtoList = toStatisticsAssetCreateDtoList(order, allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        if (CollectionUtils.isEmpty(dtoList)) {
            return true;
        } else {
            statisticsAssetService.saveBatchStatisticsAsset(dtoList);
        }
        return true;
    }

    /**
     * 公务车
     *
     * @param allVehicleNameMap
     * @param city
     * @param storeMap
     * @param allStoreByCarsMap
     * @param entrepotStorage
     * @return
     */
    private Boolean getOfficialVehicle(Map<Integer, String> allVehicleNameMap, Map<Integer, String> city, Map<Integer, Integer> storeMap, Map<Integer, StoreDto> allStoreByCarsMap, Map<Integer, EntrepotStorageDto> entrepotStorage) {
        List<StatisticsAssetOrderDto> order = statisticsAssetService.geOfficialCarStatisticsAssetOrderDto();
        log.info("查询公务车数据:{}" + order);
        List<StatisticsAssetCreateDto> dtoList = toStatisticsAssetCreateDtoList(order, allVehicleNameMap, city, storeMap, allStoreByCarsMap, entrepotStorage);
        if (CollectionUtils.isEmpty(dtoList)) {
            return true;
        } else {
            statisticsAssetService.saveBatchStatisticsAsset(dtoList);
        }
        return true;
    }

    /**
     * @param dtoList           dto
     * @param allVehicleNameMap 车型
     * @param city              城市
     * @param storeMap          门店关系
     * @param allStoreByCarsMap 门店
     * @param entrepotStorage   中转库
     * @return
     */
    private List<StatisticsAssetCreateDto> toStatisticsAssetCreateDtoList(List<StatisticsAssetOrderDto> dtoList,
                                                                          Map<Integer, String> allVehicleNameMap,
                                                                          Map<Integer, String> city,
                                                                          Map<Integer, Integer> storeMap,
                                                                          Map<Integer, StoreDto> allStoreByCarsMap,
                                                                          Map<Integer, EntrepotStorageDto> entrepotStorage) {
        List<StatisticsAssetCreateDto> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(dtoList)) {
            return list;
        }
        for (StatisticsAssetOrderDto orderDto : dtoList) {
            StatisticsAssetCreateDto dto = new StatisticsAssetCreateDto();
            BeanUtils.copyProperties(orderDto, dto);
            dto.setVehicleModel(allVehicleNameMap.get(orderDto.getVehicleId()));
            if (!StringUtils.isBlank(orderDto.getOrderCode())) {
                String specification =
                        OrderPaymentMethodEnum.getName(orderDto.getPaymentMethod())
                                + "-" + orderDto.getTransportTime() + "个月-"
                                + "-" + BuyBackEnum.getName(orderDto.getIsBuyback());
                dto.setSpecification(specification);
            }
            //设置中转库名称
            if (orderDto.getStockType() != null && orderDto.getStockType().intValue() == StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue()) {
                EntrepotStorageDto entrepotStorageDto = entrepotStorage.get(orderDto.getStockId());
                if (entrepotStorageDto != null) {
                    dto.setStockName(entrepotStorageDto.getShortName());
                    dto.setCity(city.get(entrepotStorageDto.getRegionId()));
                }
                //门店
            } else if (orderDto.getStockType() != null && orderDto.getStockType().intValue() == StockCarStockTypeEnum.STORE.getValue()
                    && !CollectionUtils.isEmpty(storeMap) &&
                    !CollectionUtils.isEmpty(allStoreByCarsMap)
                    ) {
                Integer integer = storeMap.get(orderDto.getStockId());
                if (integer != null) {
                    StoreDto storeDto = allStoreByCarsMap.get(integer);
                    if (storeDto != null) {
                        dto.setStockName(storeDto.getShortName());
                        dto.setCity(city.get(storeDto.getRegionId()));
                    }
                }
            }
            list.add(dto);
        }
        return list;
    }

    /**
     * 获取资产添加Dto
     *
     * @param carAssetDtoList
     * @return
     * @throws Exception
     */
    private List<StatisticsAssetCreateDto> getStatisticsAssetList(List<CarAssetDto> carAssetDtoList, Integer useType,
                                                                  Integer rentalType, Integer rollbackOverdueType, Integer inStockType) throws Exception {
        if (null == carAssetDtoList || carAssetDtoList.size() < 1) {
            return null;
        }
        LocalDateTime firstDayOfMonth = DateUtil.getDayStart(DateUtil.firstDayOfMonth(LocalDateTime.now().minusMonths(1)));
        LocalDateTime lastDayOfMonth = DateUtil.getDayEnd(DateUtil.lastDayOfMonth(LocalDateTime.now().minusMonths(1)));

        List<Integer> userIdList = carAssetDtoList.stream().map(CarAssetDto::getUserId).collect(Collectors.toList());
        List<Integer> storeIdList = carAssetDtoList.stream().map(CarAssetDto::getPlaceStoreId).collect(Collectors.toList());
        List<Integer> vehicleIdList = carAssetDtoList.stream().map(CarAssetDto::getVehicleId).collect(Collectors.toList());
        List<Integer> stockIdList = carAssetDtoList.stream().map(CarAssetDto::getStockId).collect(Collectors.toList());

        Map<Integer, UserDto> userMap = userService.getUsers(userIdList);
        Map<Integer, DealerStoreInfoBizDto> dealerStoreInfoMap = storeBizService.getDealerStoreInfoMap(storeIdList);
        Map<Integer, String> vehicleMap = vehicleBizService.getVehicleFullName(vehicleIdList);
        Map<Integer, EntrepotStorageDto> entrepotStorageMap = entrepotStorageService.getEntrepotStorageList(stockIdList);

        List<StatisticsAssetCreateDto> statisticsAssetCreateDtoList = new ArrayList<>();
        for (CarAssetDto carAssetDto : carAssetDtoList) {
            StatisticsAssetCreateDto statisticsAssetCreateDto = new StatisticsAssetCreateDto();
            BeanUtils.copyProperties(carAssetDto, statisticsAssetCreateDto);
            statisticsAssetCreateDto.setUseType(useType);
            statisticsAssetCreateDto.setRentalType(rentalType);
            statisticsAssetCreateDto.setRollbackOverdueType(rollbackOverdueType);
            statisticsAssetCreateDto.setInStockType(inStockType);
            statisticsAssetCreateDto.setStatisticsStartTime(firstDayOfMonth);
            statisticsAssetCreateDto.setStatisticsEndTime(lastDayOfMonth);

            Integer userId = carAssetDto.getUserId();
            Integer placeStoreId = carAssetDto.getPlaceStoreId();
            Integer vehicleId = carAssetDto.getVehicleId();
            Integer stockId = carAssetDto.getStockId();
            Integer transportTime = carAssetDto.getTransportTime();
            Integer paymentMethod = carAssetDto.getPaymentMethod();
            Boolean isBuyback = carAssetDto.getIsBuyback();
            Integer baseMileage = carAssetDto.getBaseMileage();

            if (userId != null && userMap != null && userMap.size() > 0) {
                UserDto userDto = userMap.get(userId);
                if (userDto != null) {
                    statisticsAssetCreateDto.setUserName(userDto.getRealName());
                    statisticsAssetCreateDto.setUserPhone(userDto.getPhone());
                }
            }
            if (placeStoreId != null && dealerStoreInfoMap != null && dealerStoreInfoMap.size() > 0) {
                DealerStoreInfoBizDto dealerStoreInfoBizDto = dealerStoreInfoMap.get(placeStoreId);
                if (dealerStoreInfoBizDto != null) {
                    statisticsAssetCreateDto.setDealerName(dealerStoreInfoBizDto.getDealerShortName());
                }
            }
            if (vehicleId != null && vehicleMap != null && vehicleMap.size() > 0) {
                String vehicleName = vehicleMap.get(vehicleId);
                if (vehicleName != null) {
                    statisticsAssetCreateDto.setVehicleModel(vehicleName);
                }
            }
            if (stockId != null && entrepotStorageMap != null && entrepotStorageMap.size() > 0) {
                EntrepotStorageDto entrepotStorageDto = entrepotStorageMap.get(stockId);
                if (entrepotStorageDto != null) {
                    statisticsAssetCreateDto.setStockName(entrepotStorageDto.getShortName());
                }
            }
            if (isBuyback != null) {
                statisticsAssetCreateDto.setSpecification(transportTime + "个月-"
                        + (OrderPaymentMethodEnum.INSTALLMENT.equals(paymentMethod) ? "月付"
                        : OrderPaymentMethodEnum.getName(paymentMethod))
                        + "-" + (isBuyback ? "回购" : "不回购"));
            }
            if (baseMileage != null) {
                statisticsAssetCreateDto.setSpecification(transportTime + "个月-"
                        + (OrderPaymentMethodEnum.INSTALLMENT.equals(paymentMethod) ? "月付"
                        : OrderPaymentMethodEnum.getName(paymentMethod))
                        + "-" + baseMileage + "公里");
            }
            statisticsAssetCreateDtoList.add(statisticsAssetCreateDto);
        }
        return statisticsAssetCreateDtoList;
    }

    /**
     * 添加资产
     *
     * @param statisticsAssetCreateDtoList
     * @return
     */
    private Boolean createStatisticsAsset(List<StatisticsAssetCreateDto> statisticsAssetCreateDtoList) {
        if (null == statisticsAssetCreateDtoList || statisticsAssetCreateDtoList.size() < 1) {
            return true;
        }
        statisticsAssetService.saveBatchStatisticsAsset(statisticsAssetCreateDtoList);
        return true;
    }

}
